//
//  SleepDataBarCell.swift
//  DaRings
//
//  Created by 尹琼 on 2022/11/4.
//

import UIKit
import MYModule
import JXSegmentedView
import RxSwift
import RxCocoa

open class SleepSegementedChartsCell: SleepBaseCell {
    
    let itemHeight = 295
    let leadingOffset = 6
    let pointViewWidth = 38
    
    var viewModel: SleepSegementedChartsViewModel? {
        
        didSet {
            
            guard let viewModel = viewModel else {
                return
            }
 
            segmentedView.isHidden = !viewModel.revealAll
           
            if !viewModel.revealAll {
                
                dayChartView.snp.makeConstraints { make in
                    make.top.equalToSuperview()
                    make.trailing.equalToSuperview()
                    make.leading.equalToSuperview().offset(leadingOffset)
                    make.height.equalTo(itemHeight)
                    make.bottom.equalToSuperview()
                }
            }
        }
    }
    
    public let dayChartView = SleepDataLineChartView()
    public let weekChartView = SleepDataBarChartView()
    public let monthChartView = SleepDataBarChartView()
    
    public let segmentedView = JXSegmentedView()
    
    open override func makeUI() {
        super.makeUI()
        
        bgView.cornerRadius = 0
        bgView.backgroundColor =  UIColor.darkBackground
        
        segmentedView.commonConfig(backgroundColor: UIColor.background)
        segmentedView.dataSource = dataSource
        segmentedView.indicators = [indicator]
        segmentedView.delegate = self
        
        monthChartView.isHidden = true
        weekChartView.isHidden = true
        monthChartView.backgroundColor = UIColor.background
        monthChartView.setChartConfig(self.barChartConfig())
        weekChartView.backgroundColor = UIColor.background
        weekChartView.setChartConfig(self.barChartConfig())
        
        bgView.addSubview(segmentedView)
        
        bgView.addSubview(monthChartView)
        bgView.addSubview(weekChartView)
        bgView.addSubview(dayChartView)
        
        contentView.addSubview(pointView)
        
        pointView.rx.panGesture().when(.changed).subscribe(onNext:{ [weak self] gesture in
            guard let `self` = self else {return}
            
            let point = gesture.location(in: self)
        
            self.dayChartView.slider(point, inView: self)
            self.weekChartView.slider(point)
            self.monthChartView.slider(point)
            
            let w = self.pointView.width/2 - 2
            
            if point.x < w || point.x > kScreenWidth - w {
                return
            }
            self.pointView.centerX = point.x
            
        }).disposed(by: rx.disposeBag)
        
        
        let sliderEnd = pointView.rx.panGesture().when(.ended)
        
        sliderEnd.delay(RxTimeInterval.seconds(3), scheduler: MainScheduler()).subscribe(onNext: {[weak self] _ in
            
            guard let self = self else { return }
            
            self.pointView.left = 0
            
            self.dayChartView.clearHighlight()
            self.weekChartView.clearHighlight()
            self.weekChartView.clearHighlight()
            
            self.viewModel?.dayChart.value?.sliderEnd.onNext(true)
            self.viewModel?.weekChart.value?.sliderEnd.onNext(true)
            self.viewModel?.monthChart.value?.sliderEnd.onNext(true)
            
        }).disposed(by: rx.disposeBag)
        
        
        segmentedView.snp.makeConstraints { make in
    
            make.top.leading.trailing.equalToSuperview()
            make.height.equalTo(69)
            
        }
        
        monthChartView.snp.makeConstraints { make in
            
            make.top.equalTo(segmentedView.snp.bottom)
            make.leading.trailing.equalToSuperview()
            make.height.equalTo(itemHeight)
            make.bottom.equalToSuperview()
        }
        
        weekChartView.snp.makeConstraints { make in
            make.edges.equalTo(monthChartView)
        }
        
        dayChartView.snp.makeConstraints { make in
            make.top.equalTo(segmentedView.snp.bottom)
            make.trailing.equalToSuperview()
            make.leading.equalToSuperview().offset(leadingOffset)
            make.height.equalTo(itemHeight)
            make.bottom.equalToSuperview()
        }
    }
    
    lazy var pointView: DataPointView = {
        let view = DataPointView(frame: CGRect(x: 0, y: 44 + 55 + 85, width: pointViewWidth, height: 180))
        view.pointColors = UIColor.Sleep.assist_slider
        view.imgColor = UIColor.background

        return view
    }()
    
    lazy var dataSource: JXSegmentedTitleDataSource = {
        
        let dataSource = JXSegmentedTitleDataSource()
        dataSource.commonConfig(titles: ["sleep_day_title".language,"sleep_week_title".language,"sleep_month_title".language])
        
        return dataSource
    }()
    
    var indicator: JXSegmentedIndicatorBackgroundView {
        
        let indicator = JXSegmentedIndicatorBackgroundView()
        indicator.commonConfig(indicatorColor: UIColor.Sleep.theme, shadowColor: UIColor.assist_222222)
        return indicator
    }
    
   
    func barChartConfig() -> DFBarChartViewConfig  {
        
        
        let lineColor = UIColor.assist_FFFFFF
        
        return DFBarChartViewConfig(xAxisLabelFont: Fonts.systemFont(ofSize: 11),
                                    xAxisLabelTextColor: UIColor.assist_D0D0D0,
                                    xAxisLineColor: lineColor, highlightPerTapEnabled: false)
        
    }
    
    public override func bind(to cellType: SleepViewModel.CellType) {
        super.bind(to: cellType)
        
        guard let viewModel = cellType.viewModel as? SleepSegementedChartsViewModel  else { return }
        
        self.viewModel = viewModel
        
        let disposeBag = DisposeBag()
        viewModel.dayChart.subscribe(onNext: {[weak self] in
            
            guard let viewModel = $0 else { return }
            self?.dayChartView.bind(by: viewModel)
            
        }).disposed(by: disposeBag)
        
        viewModel.weekChart.subscribe(onNext: {[weak self] in
            
            guard let viewModel = $0 else { return }
           
            self?.weekChartView.bind(by: viewModel)
            
        }).disposed(by: disposeBag)
        
        viewModel.monthChart.subscribe(onNext: {[weak self] in
            
            guard let viewModel = $0 else { return }
            self?.monthChartView.bind(by: viewModel)
            
        }).disposed(by: disposeBag)

    }
    
   
    
    open override func layoutSubviews() {
        super.layoutSubviews()
        var frame = pointView.frame
        if frame.maxY != contentView.bounds.height {
            frame.origin.y = contentView.bounds.height - frame.size.height
            self.pointView.frame = frame
           
        }
        
    }
    
}

extension SleepSegementedChartsCell: JXSegmentedViewDelegate {
    
    public func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
        
        dayChartView.isHidden = index != 0
        weekChartView.isHidden = index != 1
        monthChartView.isHidden = index != 2
        
    }
    
}
